2 Accepted, but the fastest is 11590.7.cpp
25 #define D(x) cout << #x " = " << (x) << endl
27 typedef unsigned long long Int
;
28 const int BUFSIZE
= 256;
36 bool end
; //is this node a complete prefix?
37 node(bool e
) : end(e
) {
42 //not used, because I love to waste memory (makes me feel sexy)
44 if (n
== NULL
) return;
45 clean(n
->left
); clean(n
->right
);
49 Int
f(string s
, node
* n
){ //s = string built so far, n = the node we are standing at
50 Int left
= 0, right
= 0, ret
= 0;
52 left
= f(s
+ "0", n
->left
);
54 if (n
->right
!= NULL
){
55 right
= f(s
+ "1", n
->right
);
61 if (M
- s
.size() < 64){ //we are safe from overflow
62 Int x
= ((1ULL) << (M
- s
.size())); //2^(m - |s|)
63 howmany
= x
- left
- right
;
64 }else{ //2^64 overflows!
66 x
= ~x
; // (x = ~0ULL) <-> (x = 2^64 - 1)
67 howmany
= x
- left
- right
+ 1;
78 assert(scanf("%d %d ", &n
, &M
) == 2);
79 if (n
== 0 && M
== 0) break;
82 node
* root
= new node(true);
84 fgets(buf
, BUFSIZE
, stdin
);
92 if (cur
->left
== NULL
) cur
->left
= new node(false);
94 }else if (buf
[i
] == '1'){
95 if (cur
->right
== NULL
) cur
->right
= new node(false);
106 fgets(buf
, BUFSIZE
, stdin
);
107 int end
= strlen(buf
)-1;
108 while (buf
[end
] != '*') end
--;
110 Int t
= ans
[string(buf
)];
115 scanf(" "); //empty line